<?php

class links_submit
{
	var $mydirname;
	var $controller_name;
	var $Links;

	/** Constructor
	 *
	**/
	function links_submit($mydirname)
	{
		$this->mydirname = $mydirname;
		$this->controller_name = str_replace('links_', '', get_class($this));
		$this->Links = links_Base::getInstance($this->mydirname);
		$this->Links->setControllerName($this->controller_name);
		$this->Links->breadcrumbs();

		if (! $this->Links->post_perm) {
			$this->Links->linksRedirectHeader(XOOPS_URL .'/user.php', 2, _NOPERM);
			exit();
		}
	}

	function index($cid = 0)
	{
		$this->form($cid);
	}

	function form($cid = 0)
	{
		//category exists
		if (!is_array($this->Links->allcid) || count($this->Links->allcid) == 0) {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_NO_CATEGORY);
			exit;
		}

		$myts =& MyTextSanitizer::getInstance();

		//Form Bookmarklet
		$site_title = isset($_GET['sitetitle']) ? $this->Links->stripSlashesGPC($_GET['sitetitle']): '';
		$site_url   = isset($_GET['siteurl'])   ? $this->Links->stripSlashesGPC($_GET['siteurl'])  : '';
		if (function_exists('mb_convert_encoding')) {
			$site_title = mb_convert_encoding($site_title, _CHARSET, 'auto');
		//	$site_title = mb_convert_encoding($site_title, _CHARSET, mb_detect_encoding($site_title));
		}
		if(!empty($site_title)) $site_title = $myts->makeTareaData4Edit($site_title);
		if(!empty($site_url)) $site_url = htmlspecialchars(links_urlCheckReplace($site_url), ENT_QUOTES, _CHARSET);

		//for RETURN after checkURI
		$murl = $guestname = $adult = $desc = '' ;
		if (isset($_SESSION[$this->mydirname])) {
		//	$myts =& MyTextSanitizer::getInstance();
			$site_title = $myts->makeTareaData4Edit($_SESSION[$this->mydirname]['site_title']);
		//	$site_title = htmlspecialchars($_SESSION[$this->mydirname]['site_title'],ENT_QUOTES, _CHARSET);
			$site_url   = htmlspecialchars(links_urlCheckReplace( $_SESSION[$this->mydirname]['site_url']) , ENT_QUOTES, _CHARSET);
			$murl       = htmlspecialchars(links_urlCheckReplace( $_SESSION[$this->mydirname]['murl']) , ENT_QUOTES, _CHARSET);
			$guestname  = htmlspecialchars($_SESSION[$this->mydirname]['guestname'], ENT_QUOTES, _CHARSET);
			$adult      = intval($_SESSION[$this->mydirname]['adult']);
			$desc       = $myts->makeTareaData4Edit($_SESSION[$this->mydirname]['desc']);
			unset($_SESSION[$this->mydirname]); 
		}

		$multiple = $this->Links->is_mobile ? 1: $this->Links->category_selector;
		$category_selbox = $this->Links->Categories->makeMySelBox('corder,cid', $cid, !$multiple, 'cid[]', '', 'cid', $multiple);

		$data = array(
			'category_selbox' => $category_selbox,
			'sitetitle'       => $site_title,
			'siteurl'         => $site_url,
			'murl'            => $murl,
			'guestname'       => $guestname,
			'adult'           => $adult,
			'desc'            => $desc,
			'date_selector'   => $this->Links->makeDateSelector(),
			'gticket'         => $GLOBALS['xoopsGTicket']->getTicketHtml(__LINE__, 1800, 'links_submit'),
		);

		$template = $this->Links->getTemplatePrefix() . '_submit.html';
		$this->Links->view($template, $data);

	}

	function post()
	{
		$js_check = $this->Links->is_mobile ? FALSE: TRUE;
		$this->Links->basicPostCheck('links_submit', $js_check);

		if ($this->Links->is_mobile && $this->Links->uid == 0) {
			$ip = getenv('REMOTE_ADDR');
			if (empty($ip)) {
				$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 3, 'New Link Post Error: '._MD_ANY_PROBLEM);
				exit();
			}
			// POST TIME GAP CHECK
			$this->Links->clearWhere();
			$this->Links->setWhere('ip', $ip, '=');
			$this->Links->setWhere('date', time()-300, '>'); // 5 minutes
			$count = $this->Links->getCount() ;
			if ($count > 0) {
				$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 3, _MD_TIME_GAP_SMALL);
				exit();
			}
		}

		if ($this->Links->insert()) {
			// Delegate
			if (class_exists('XCube_DelegateUtils')) {
				XCube_DelegateUtils::call('Modules.Links.Event.Submit.InsertSuccess', new XCube_Ref($this));
			}

			if ($this->Links->post_approval == TRUE) {
				$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_RECEIVED ."\n". _MD_ISAPPROVED . $this->Links->status_code);
				exit;
			} else {
				$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_RECEIVED);
				exit;
			}
		} else {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 5, 'DB ERROR'); //TODO
			exit;
		}
	}


}